package com.hcrazy.manchester.app.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.form.FoodSearchForm;
import com.hcrazy.manchester.app.pojo.BookFood;
import com.hcrazy.manchester.app.pojo.BookMenu;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Seller;
import com.hcrazy.manchester.app.services.BookFoodService;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.SellerService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/book")
public class BookFoodController {
	
	@Autowired
	private BookFoodService foodService;
	
	@Autowired
	private SellerService sellerService;
	
	@Autowired
	private BookMenuService bookMenuService;
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addFood",method=RequestMethod.GET)
	public void addBookFood(Model model){
		setSellers(model);
		//初始化新增对象
		model.addAttribute("food", new BookFood());
		
	}
	
	/**
	 * 编辑信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/editFood",method=RequestMethod.GET)
	public String editBookFood(@RequestParam long foodId,Model model){
		try {
			//初始化对象内容
			addBookFood(model);
			//根据id获取信息内容
			BookFood food = foodService.getBookFoodById(foodId);
			//更新返回对象
			model.addAttribute("food",food);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/book/addFood";
	}
	
	/**
	 * 提交信息内容
	 * @param food
	 * @return
	 */
	@RequestMapping(value="/food",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateSort(@ModelAttribute("form") BookFood food,
    		Model model){
		try {
			foodService.addOrUpdateBookFood(food);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/book/food";
    }
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/food",method=RequestMethod.GET)
	public void food(Model model,HttpServletRequest req){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllBookFoods(1,0,0,"");
		model.addAttribute("pageModel", pageModel);
		
		//初始化分类
		setSellers(model);
		
		//初始化查询对象
		model.addAttribute("searchForm", new FoodSearchForm());
		
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_food",method=RequestMethod.POST)
	public String searchAddress(@ModelAttribute("form") FoodSearchForm form,
			Model model,HttpServletRequest req){
		//是否执行删除操作
		if(form.getFoodId() > 0){
			try {
				foodService.delBookFood(form.getFoodId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setFoodId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllBookFoods(form.getPageNum(),form.getSellerId(),form.getMenuId(),form.getSearch());
		model.addAttribute("pageModel", pageModel);

		//设置查询类别
		setSellers(model);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/book/food";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllBookFoods(int pageNum,long sellerId,int menuId,String search){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		
		try {
			return foodService.getBookFoods(model, sellerId, menuId, search,0,Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}

	private void setSellers(Model model){
		//根据功能码设置分类列表
		try {
			List<Seller> sellers = sellerService.getAllSellers();
			model.addAttribute("sellers", sellers);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}		
	}
	
	@RequestMapping(value="/menu/tree",produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<BookMenu> setMenus(@RequestParam long sellerId){
		try {
			List<BookMenu> menus = bookMenuService.getAllMenus(Constant.TYPE_BOOK_FOOD_MENU, sellerId);
			return menus;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}		
	}
}
